package org.xwalk.core.internal;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class XWalkExtensionBridge extends XWalkExtensionInternal {
    private XWalkCoreBridge coreBridge;
    private Object wrapper;

    public Object getWrapper() {
        return wrapper;
    }



    public XWalkExtensionBridge(String name, String jsApi, Object wrapper) {
        super(name, jsApi);

        this.wrapper = wrapper;
        reflectionInit();

    }

        public XWalkExtensionBridge(String name, String jsApi, String[] entryPoints, Object wrapper) {
        super(name, jsApi, entryPoints);

        this.wrapper = wrapper;
        reflectionInit();

    }

        @Override
    public void postMessage(int instanceID, String message) {
        if (postMessageintStringMethod == null || postMessageintStringMethod.isNull()) {
            postMessageSuper(instanceID, message);
        } else {
            postMessageintStringMethod.invoke(instanceID, message);
        }
    }

    public void postMessageSuper(int instanceID, String message) {
        super.postMessage(instanceID, message);
    }

    private ReflectMethod postMessageintStringMethod = new ReflectMethod(null, "postMessage");

    @Override
    public void postBinaryMessage(int instanceID, byte[] message) {
        if (postBinaryMessageintbyteArrayMethod == null || postBinaryMessageintbyteArrayMethod.isNull()) {
            postBinaryMessageSuper(instanceID, message);
        } else {
            postBinaryMessageintbyteArrayMethod.invoke(instanceID, message);
        }
    }

    public void postBinaryMessageSuper(int instanceID, byte[] message) {
        super.postBinaryMessage(instanceID, message);
    }

    private ReflectMethod postBinaryMessageintbyteArrayMethod = new ReflectMethod(null, "postBinaryMessage");

    @Override
    public void broadcastMessage(String message) {
        if (broadcastMessageStringMethod == null || broadcastMessageStringMethod.isNull()) {
            broadcastMessageSuper(message);
        } else {
            broadcastMessageStringMethod.invoke(message);
        }
    }

    public void broadcastMessageSuper(String message) {
        super.broadcastMessage(message);
    }

    private ReflectMethod broadcastMessageStringMethod = new ReflectMethod(null, "broadcastMessage");

    @Override
    public void onInstanceCreated(int instanceID) {
        if (onInstanceCreatedintMethod == null || onInstanceCreatedintMethod.isNull()) {
            onInstanceCreatedSuper(instanceID);
        } else {
            onInstanceCreatedintMethod.invoke(instanceID);
        }
    }

    public void onInstanceCreatedSuper(int instanceID) {
        super.onInstanceCreated(instanceID);
    }

    private ReflectMethod onInstanceCreatedintMethod = new ReflectMethod(null, "onInstanceCreated");

    @Override
    public void onInstanceDestroyed(int instanceID) {
        if (onInstanceDestroyedintMethod == null || onInstanceDestroyedintMethod.isNull()) {
            onInstanceDestroyedSuper(instanceID);
        } else {
            onInstanceDestroyedintMethod.invoke(instanceID);
        }
    }

    public void onInstanceDestroyedSuper(int instanceID) {
        super.onInstanceDestroyed(instanceID);
    }

    private ReflectMethod onInstanceDestroyedintMethod = new ReflectMethod(null, "onInstanceDestroyed");

    @Override
    public void onBinaryMessage(int instanceID, byte[] message) {
        if (onBinaryMessageintbyteArrayMethod == null || onBinaryMessageintbyteArrayMethod.isNull()) {
            onBinaryMessageSuper(instanceID, message);
        } else {
            onBinaryMessageintbyteArrayMethod.invoke(instanceID, message);
        }
    }

    public void onBinaryMessageSuper(int instanceID, byte[] message) {
        super.onBinaryMessage(instanceID, message);
    }

    private ReflectMethod onBinaryMessageintbyteArrayMethod = new ReflectMethod(null, "onBinaryMessage");

    @Override
    public void onMessage(int instanceID, String message) {
        onMessageintStringMethod.invoke(instanceID, message);
    }


    private ReflectMethod onMessageintStringMethod = new ReflectMethod(null, "onMessage");

    @Override
    public String onSyncMessage(int instanceID, String message) {
        return (String)onSyncMessageintStringMethod.invoke(instanceID, message);
    }


    private ReflectMethod onSyncMessageintStringMethod = new ReflectMethod(null, "onSyncMessage");


    void reflectionInit() {
        coreBridge = XWalkCoreBridge.getInstance();
        if (coreBridge == null) return;

        postMessageintStringMethod.init(wrapper, null,
                "postMessage", int.class, String.class);
        postBinaryMessageintbyteArrayMethod.init(wrapper, null,
                "postBinaryMessage", int.class, byte[].class);
        broadcastMessageStringMethod.init(wrapper, null,
                "broadcastMessage", String.class);
        onInstanceCreatedintMethod.init(wrapper, null,
                "onInstanceCreated", int.class);
        onInstanceDestroyedintMethod.init(wrapper, null,
                "onInstanceDestroyed", int.class);
        onBinaryMessageintbyteArrayMethod.init(wrapper, null,
                "onBinaryMessage", int.class, byte[].class);
        onMessageintStringMethod.init(wrapper, null,
                "onMessage", int.class, String.class);
        onSyncMessageintStringMethod.init(wrapper, null,
                "onSyncMessage", int.class, String.class);
    }
}
